package fragment.menufragments;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.MenuListEntity;
import fragment.AbsBaseFragment;
import tools.ItemOnClickListenerOther;
import tools.PassingParametersInterface;
import volley.VolleySingleton;

/**
 * 播放列表
 * Created by dllo on 15/11/27.
 */
public class MenuListFragment extends AbsBaseFragment {
    private ListView listView;
    private String URL;
    private MenuListAdapter adapter;
    private ImageView titleForAllBack;
    private TextView titleForAllTv;
    private List list;
    private PassingParametersInterface anInterface;



    public MenuListFragment(List list) {
        this.list = list;
    }

    public void getParameters(PassingParametersInterface anInterface) {
        this.anInterface = anInterface;
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_list_menu;
    }

    @Override
    protected void initView() {
        titleForAllBack = findView(R.id.titleForAllBack);
        titleForAllTv = findView(R.id.titleForAllTv);
        listView = findView(R.id.menuListView);
    }

    @Override
    protected void initData() {
        titleForAllTv.setText("播放列表");
        titleForAllBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        adapter = new MenuListAdapter(getContext());
        adapter.setItemOnClick(new ItemOnClickListenerOther() {
            @Override
            public void itemListener(Object o, int i) {
                MenuListEntity.DataEntity d = (MenuListEntity.DataEntity) o;
                switch (i) {
                    case 0:
                        Toast.makeText(getContext(), d.getDownloadUrl(), Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        if (anInterface != null)
                            anInterface.passingParameters(d.getTrackId());
                        goBack();
                        break;
                }
            }
        });
        listView.setAdapter(adapter);
        adapter.addList(list);
    }

    /**
     * listView adapter
     */
    private class MenuListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private Context context;
        private List<MenuListEntity.DataEntity> entities;
        private ItemOnClickListenerOther listener;

        public MenuListAdapter(Context context) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }

        public void addList(List<MenuListEntity.DataEntity> entities) {
            this.entities = entities;
            notifyDataSetChanged();
        }

        public void setItemOnClick(ItemOnClickListenerOther listener) {
            this.listener = listener;
        }

        private boolean isListNull() {
            return entities != null && entities.size() > 0;
        }

        @Override
        public int getCount() {
            return isListNull() ? entities.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            MenuViewHolder holder;
            //观察convertView随ListView滚动情况
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.fragment_list_menu_item, parent, false);
                holder = new MenuViewHolder();
                /*得到各个控件的对象*/
                holder.menuListTitle = (TextView) convertView.findViewById(R.id.menuListTitle);
                holder.menuListDownload = (ImageView) convertView.findViewById(R.id.menuListDownload);
                convertView.setTag(holder);//绑定ViewHolder对象
            } else {
                holder = (MenuViewHolder) convertView.getTag();//取出ViewHolder对象
            }
            if (isListNull()) {
                final MenuListEntity.DataEntity entity = entities.get(position);
                holder.menuListTitle.setText(entity.getTitle());
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null)
                            listener.itemListener(entity, 1);
                    }
                });
                holder.menuListDownload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null)
                            listener.itemListener(entity, 0);
                    }
                });
            }
            return convertView;
        }

        private class MenuViewHolder {
            TextView menuListTitle;
            ImageView menuListDownload;
        }
    }
}
